package com.tul.exercise.base.innerclass.chapter10_2;



/**
 * @Description 《java编程思想》10.2 示例 1
 *  内部类能访问外围对象的所有成员，不需要任何特殊条件
 **/
public class Sequence implements Selector{

    // 存储对象，固定长度数组
    private Object[] items;

    // 下一个对象位置
    private int next = 0;

    public Sequence(int size) {
        items = new Object[size];
    }

    /**
     * 添加元素方法
     * @param x 元素对象
     */
    public void add(Object x) {
        if (next < items.length) {
            items[next++] = x;
        }
    }

    private class  SequenceSelector implements Selector {
        // 当前元素指示器
        private int i = 0;

        @Override
        public boolean end() {
            return i == items.length;
        }

        @Override
        public Object current() {
            return items[i];
        }

        @Override
        public void next() {
            if (i < items.length) {
                i++;
            }
        }
    }

    public Selector selector() {
        return new SequenceSelector();
    }

    @Override
    public boolean end() {
        return false;
    }

    @Override
    public Object current() {
        return null;
    }

    @Override
    public void next() {

    }
}
